package com.letoken.platform.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.admin.service.AdminRoleService;
import com.letoken.platform.pub.constant.AdminRoleConstant;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.repo.admin.dao.AdminRoleMapper;
import com.letoken.platform.repo.admin.po.AdminRole;
import com.letoken.platform.repo.admin.req.config.AddAdminRoleReq;
import com.letoken.platform.repo.admin.req.config.QueryAdminRoleReq;
import com.letoken.platform.repo.admin.req.config.UpdateAdminRoleReq;
import com.letoken.platform.repo.admin.res.user.QueryAdminRoleByIdRes;
import com.letoken.platform.repo.admin.res.user.QueryAdminRoleRes;
import com.letoken.platform.repo.utlis.PageDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author wyl
 * @Description
 * @date 2022/12/13 18:54
 * @Version 1.0
 */
@Service
public class AdminRoleServiceImpl implements AdminRoleService {
    /**
     * 查询角色列表
     *
     * @param req
     * @return
     */

    @Autowired
    private AdminRoleMapper adminRoleMapper;


    /**
     * 查询角色列表
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryAdminRoleRes> queryAdminRole(QueryAdminRoleReq req) {
        Page<QueryAdminRoleRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryAdminRoleRes> res = adminRoleMapper.queryAdminRole(page, req);
        return PageDataUtil.getPageData(res);
    }

    /**
     * 根据id查询角色
     * @param adminRoleId
     * @return
     */
    @Override
    public QueryAdminRoleByIdRes queryAdminRoleById(Integer adminRoleId) {
        QueryAdminRoleByIdRes res = adminRoleMapper.queryAdminRoleById(adminRoleId);
        res.setAdminRoleId(adminRoleId);
        return res;
    }

    /**
     * 根据id删除角色
     * @param adminRoleId
     * @return
     */
    @Override
    public void deleteAdminRole(Integer adminRoleId) {
        adminRoleMapper.deleteById(adminRoleId);
    }

    /**
     * 新增角色
     *
     * @param req
     * @return
     */
    @Override
    public void addAdminRole(AddAdminRoleReq req) {
        AdminRole adminRole = new AdminRole();
        BeanUtil.quickCopy(req, adminRole);
        adminRole.setPid(AdminRoleConstant.ONE_PID);
        adminRole.setCreateTime(LocalDateTime.now());
        adminRoleMapper.insert(adminRole);
    }


    /**
     * 修改角色
     *
     * @param req
     * @return
     */
    @Override
    public void updateAdminRole(UpdateAdminRoleReq req) {
        AdminRole adminRole = new AdminRole();
        BeanUtil.quickCopy(req, adminRole);
        adminRole.setUpdateTime(LocalDateTime.now());
        adminRoleMapper.updateById(adminRole);
    }


}
